﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class MenuEditor : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null || ((Entity.UserInfo)Session["UserInfo"]).UserType != "admin")
            {
                Response.Redirect("~/Search.aspx");
            }

            if (!IsPostBack)
            {
                DataTable Taginfo = new DataTable();
                SQLHelper.getTagInfo(out Taginfo);
                for (int i = 0; i < Taginfo.Rows.Count; i++)
                {
                    if (Taginfo.Rows[i]["ParentId"].Equals(0))
                    {
                        this.ListBox1.Items.Add(new ListItem(Taginfo.Rows[i]["TagName"].ToString(), Taginfo.Rows[i]["Id"].ToString()));
                    }
                }
            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ListBox2.Items.Clear();
            this.ListBox3.Items.Clear();

            DataTable dt = new DataTable();
            SQLHelper.getChildTagsByParentId(Convert.ToInt32(ListBox1.SelectedItem.Value), out dt);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListBox2.Items.Add(new ListItem(dt.Rows[i]["TagName"].ToString(), dt.Rows[i]["Id"].ToString()));
            }

        }

        protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ListBox3.Items.Clear();

            DataTable dt = new DataTable();
            SQLHelper.getChildTagsByParentId(Convert.ToInt32(ListBox2.SelectedItem.Value), out dt);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListBox3.Items.Add(new ListItem(dt.Rows[i]["TagName"].ToString(), dt.Rows[i]["Id"].ToString()));
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            int iresult = SQLHelper.AddTag(TextBox1.Text,0,1);


            if (iresult != 1)
                Response.Write("<script>alert('新增失败!');location.href='MenuEditor.aspx';</script>");
            else 
                Response.Write("<script>alert('新增成功!');location.href='MenuEditor.aspx';</script>");
           
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int iresult = SQLHelper.AddTag(TextBox2.Text,Convert.ToInt32(ListBox1.SelectedItem.Value), 2);
            if (iresult != 1)
                Response.Write("<script>alert('新增失败!');location.href='MenuEditor.aspx';</script>");
            else
                Response.Write("<script>alert('新增成功!');location.href='MenuEditor.aspx';</script>");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            int iresult = SQLHelper.AddTag(TextBox3.Text, Convert.ToInt32(ListBox2.SelectedItem.Value), 3);
            if (iresult != 1)
                Response.Write("<script>alert('新增失败!');location.href='MenuEditor.aspx';</script>");
            else
                Response.Write("<script>alert('新增成功!');location.href='MenuEditor.aspx';</script>");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            DataTable dtSecond = new DataTable();
            SQLHelper.getChildTagsByParentId(Convert.ToInt32(ListBox1.SelectedItem.Value), out dtSecond);
            for (int ix = 0; ix < dtSecond.Rows.Count; ix++)
            {
                DataTable dtLeaf = new DataTable();
                SQLHelper.getChildTagsByParentId(Convert.ToInt32(dtSecond.Rows[ix]["Id"]), out dtLeaf);
                for (int iy = 0; iy < dtLeaf.Rows.Count; iy++)
                {
                    SQLHelper.deleteTagInfoByTagId(Convert.ToInt32(dtLeaf.Rows[iy]["Id"]));
                }
                SQLHelper.deleteTagInfoByTagId(Convert.ToInt32(dtSecond.Rows[ix]["Id"]));
            }

            int iresult = SQLHelper.deleteTagInfoByTagId(Convert.ToInt32(ListBox1.SelectedItem.Value));
            if (iresult != 1)
                Response.Write("<script>alert('删除失败!');location.href='MenuEditor.aspx';</script>");
            else
                Response.Write("<script>alert('删除成功!');location.href='MenuEditor.aspx';</script>");

        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            DataTable dtLeaf = new DataTable();
            SQLHelper.getChildTagsByParentId(Convert.ToInt32(ListBox2.SelectedItem.Value), out dtLeaf);
            for (int ix = 0; ix < dtLeaf.Rows.Count; ix++)
            {
                SQLHelper.deleteTagInfoByTagId(Convert.ToInt32(dtLeaf.Rows[ix]["Id"]));
            }

            int iresult = SQLHelper.deleteTagInfoByTagId(Convert.ToInt32(ListBox2.SelectedItem.Value));
            if (iresult != 1)
                Response.Write("<script>alert('删除失败!');location.href='MenuEditor.aspx';</script>");
            else
                Response.Write("<script>alert('删除成功!');location.href='MenuEditor.aspx';</script>");
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            int iresult = SQLHelper.deleteTagInfoByTagId(Convert.ToInt32(ListBox3.SelectedItem.Value));
            if (iresult != 1)
                Response.Write("<script>alert('删除失败!');location.href='MenuEditor.aspx';</script>");
            else
                Response.Write("<script>alert('删除成功!');location.href='MenuEditor.aspx';</script>");
        }
    }
}